{% import 'student/assignment/_formhelpers.html' as forms %}
{% import '_globalhelpers.html' as globals %}

{% macro render_subms(subms, course, flagged, flag_form, tname="Submissions", empty=True) %}
  {% if subms %}
  <div class="list hidden-xs hidden-sm">
    <div class="wrap">
      <h2>{{ tname }}</h2>
      <table>
        <tr class="header">
          <th class="col-id">ID</th>
          <th class="col-author">Author</th>
          <th class="col-flag">Flag</th>
          <th class="col-code">Code</th>
        </tr>
        {% for subm in subms %}
        <tr>
          <td>
            <a href="{{ url_for('.code', name=assignment.name, bid=subm.id, submit=subm.submit)}}" data-no-instant>
              Submission {{ globals.backup_style(subm.hashid) }}
            </a>
          </td>
          <td>
            <div class="due-dot grey"></div>
            <div class="due">
              <span class="due-text grey">{{ subm.submitter.email }}</span>
              <span class="due-date">{{ utils.local_time(subm.created, course) }}</span>
            </div>
          </td>
          <td>
            {% call forms.render_form(flag_form, action_url=url_for('.flag', name=assignment.name, bid=subm.id), class_='form-inline') %}
              {% if not subm.flagged %}
                <input type="hidden" name="flag">
              {% endif %}
              <input type="hidden" name="next" value="{{ request.url }}">
              <button class="flag-button tip-trigger final-button button-small"  {% if not assignment.active %} disabled {% endif %} type="submit">
                <div class="tip">
                  {% if not assignment.active %}
                    <span>Cannot be changed after deadline</span>
                  {% elif subm.flagged %}
                    <span>Flagged for grading (click to unflag)</span>
                  {% else %}
                    <span>Not flagged for grading (click to flag)</span>
                  {% endif %}
                </div>

                <i class="fa fa-flag final-{{ 'yes' if subm.flagged else 'no' }}"></i>
                &nbsp; {{ 'Remove Flag' if subm.flagged else 'Flag for grading' }}
              </button>
            {% endcall %}
          </td>

          <td>
              <a href="{{ url_for('.code', name=assignment.name, bid=subm.id, submit=subm.submit)}}" class="view-code no-dash button-small" data-no-instant>
                View Code
              </a>
          </td>
          </td>
        </tr>
        {% endfor %}
      </table>
    </div>
  </div>
  {% elif empty %}
  <div class="list hidden-xs hidden-sm">
    <div class="wrap">
      <h2>No {{ tname }}</h2>
    </div>
  </div>
  {% endif %}
{% endmacro %}

{% macro render_backups(backups, course, flagged, flag_form, tname="Backups", empty=True) %}
  {% if backups %}
  <div class="list hidden-xs hidden-sm">
    <div class="wrap">
      <h2>{{ tname }}</h2>
      <table>
        <tr class="header">
          <th class="col-id">ID</th>
          <th class="col-author">Author</th>
          <th class="col-submit">Submit</th>
          <th class="col-code">Code</th>
        </tr>
        {% for backup in backups %}
        <tr>
          <td>
            <a href="{{ url_for('.code', name=assignment.name, bid=backup.id, submit=backup.submit)}}" data-no-instant>
              Backup {{ globals.backup_style(backup.hashid) }}
            </a>
          </td>
          <td>
            <div class="due-dot grey"></div>
            <div class="due">
              <span class="due-text grey">{{ backup.submitter.email }}</span>
              <span class="due-date">{{ utils.local_time(backup.created, course) }}</span>
            </div>
          </td>
          <td>
            {% call forms.render_form(flag_form, action_url=url_for('.flag', name=assignment.name, bid=backup.id), class_='form-inline') %}
              <input type="hidden" name="flag">
              <input type="hidden" name="next" value="{{ request.url }}">
              <button class="convert-button tip-trigger final-button button-small"  {% if not assignment.active %} disabled {% endif %} type="submit">
                  {% if not assignment.active %}
                  <div class="tip">
                    <span>Too late to convert submission</span>
                  </div>
                  {% else %}
                  <div class="tip">
                    <span>Convert to submission & <br> flag for grading (click to flag)</span>
                  </div>
                  {% endif %}
                  Convert to submission
              </button>
            {% endcall %}
          </td>
          <td>
              <a href="{{ url_for('.code', name=assignment.name, bid=backup.id, submit=backup.submit)}}" class="no-dash button-small" data-no-instant>
                View Code
              </a>
          </td>
        </tr>
        {% endfor %}
      </table>
    </div>
  </div>
  {% elif empty %}
  <div class="list hidden-xs hidden-sm">
    <div class="wrap">
      <h2>No {{ tname }}</h2>
    </div>
  </div>
  {% endif %}
{% endmacro %}

{% macro render_subms_mobile(subms, course, flagged, flag_form, tname="Submissions", empty=True) %}
  {% if subms %}
  <div class="subcontent list hidden-md hidden-lg">
    <div class="wrap">
      <h2>{{tname}}</h2>
      {% for subm in subms %}

      <div class="cell col-xs-12">
        <div class="cell-title"><a href="{{ url_for('.code', name=assignment.name, bid=subm.id, submit=subm.submit)}}" data-no-instant> {{ utils.local_time(subm.created, course) }} </a></div>
        <div class="cell-text"><span>Submitter:  {{ subm.submitter.email }}</span></div>
        <div class="cell-status">
          <span>
          {% if subm.flagged %}
          <i class="fa fa-flag green"></i> Flagged for Grading. This submission will be graded.
          {% elif not flagged and loop.index == 1 %}

              <i class="fa fa-clock-o grey"></i> This is the most recent submission.
              Unless you flag another submission, the most recent submission will be graded</span>
              {% else %}
            <div class="due-dot grey"></div> Not flagged for grading.
          {% endif %}
        </span>

        </div>
        <hr>
        <div class="cell-footer">
            <div class="pull-left">
                <a href="{{url_for('.code', name=assignment.name, bid=subm.id, submit=subm.submit)}}" class="dashed" data-no-instant>
                    View Code
                </a>
            </div>
            <div class="pull-right">
                {% call forms.render_form(flag_form, action_url=url_for('.flag', name=assignment.name, bid=subm.id), class_='form-inline') %}
                  {% if not subm.flagged %}
                    <input type="hidden" name="flag">
                  {% endif %}
                  <input type="hidden" name="next" value="{{ request.url }}">
                  <button class="final-button" type="submit">
                      <a class="dashed">{{ 'Remove Flag for Grading' if subm.flagged else 'Flag for Grading' }} </a>
                  </button>
                {% endcall %}
            </div>
        </div>
    </div>
      {% endfor %}
    </div>
  </div>
  {% elif empty %}
  <div class="subcontent list hidden-md hidden-lg">
    <div class="wrap">
      <h2>No {{ tname }}</h2>
    </div>
  </div>
  {% endif %}
{% endmacro %}

{% macro render_backups_mobile(backups, course, flagged, flag_form, tname="Backups", empty=True) %}
  {% if backups %}
  <div class="subcontent list hidden-md hidden-lg">
    <div class="wrap">
      <h2>{{tname}}</h2>
      {% for backup in backups %}

      <div class="cell col-xs-12">
        <div class="cell-title"><a href="{{ url_for('.code', name=assignment.name, bid=backup.id, submit=backup.submit)}}" data-no-instant> {{ utils.local_time(backup.created, course) }} </a></div>
        <div class="cell-text"><span>Submitter:  {{ backup.submitter.email }}</span></div>
        <div class="cell-status">
            <div class="pull-left">
                <a href="{{url_for('.code', name=assignment.name, bid=backup.id, submit=backup.submit)}}" class="dashed" data-no-instant>
                    View Code
                </a>
            </div>
            <div class="pull-right">
                {% call forms.render_form(flag_form, action_url=url_for('.flag', name=assignment.name, bid=backup.id), class_='form-inline') %}
                  <input type="hidden" name="flag">
                  <input type="hidden" name="next" value="{{ request.url }}">
                  <button class="final-button" type="submit">
                      <i class="fa fa-flag final-no"></i> <a class="dashed">Submit & Flag for Grading</a>
                  </button>
                {% endcall %}
            </div>
        </div>
      </div>
          <div class="cell-footer">
        </div>

      {% endfor %}
    </div>
  </div>
  {% elif empty %}
  <div class="subcontent list hidden-md hidden-lg">
    <div class="wrap">
      <h2>No {{ tname }}</h2>
    </div>
  </div>
  {% endif %}
{% endmacro %}
